package team.first.movieapiv1pro.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import team.first.movieapiv1pro.domain.Genre;
import team.first.movieapiv1pro.domain.Movie;
import team.first.movieapiv1pro.domain.MovieCast;
import team.first.movieapiv1pro.dto.RequestMovie;
import team.first.movieapiv1pro.service.*;
import team.first.movieapiv1pro.vo.ApiResponse;
import team.first.movieapiv1pro.vo.ApiResponsePaging;
import team.first.movieapiv1pro.vo.ResponseInnerStaff;
import team.first.movieapiv1pro.vo.ResponseMovie;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: HAPPY
 * @Project_name: project
 * @Package_name: team.first.movieapiv1pro.controller
 * @Date: 2021/11/25 11:08
 * @Description：
 */
@RestController
public class MovieController {
    @Autowired
    MovieService movieService;
    @Autowired
    GenreService genreService;
    @Autowired
    StaffService staffService;
    @Autowired
    MovieCastService movieCastService;
    @Autowired
    MovieGenreService movieGenreService;

    /**
     * 1.2 获取指定编号的电影信息
     * 接口地址：GET /api/v1/movie/{movieId}
     */
    @ApiOperation("1.2 GET: 获取指定编号的电影信息")
    @GetMapping("/api/v1/movie/{movieId}")
    public ApiResponse<ResponseMovie> movie(@ApiParam("电影编号") @PathVariable("movieId") Integer movieId) {
        ApiResponse<ResponseMovie> apiResponse = new ApiResponse<>();
        try {
            // 先查询到指定编号的电影信息
            Movie movie = movieService.getById(movieId);

            if (movie == null) {
                // 如果没有编号为movieId的电影，返回空信息
                apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(null);
                return apiResponse;
            } else {
                // 将查询到的电影的信息拷贝到返回的电影中
                ResponseMovie responseMovie = BeanUtil.copyProperties(movie, ResponseMovie.class);

                // 查询所有电影分类信息
                List<Genre> genreList = genreService.getGenresByMovieId(movieId);
                responseMovie.setGenres(genreList);

                // 查询电影的所有演职人员
                List<ResponseInnerStaff> allInnerStaff = staffService.getAllStaffsByMovieId(movieId);
                List<ResponseInnerStaff> responseInnerActors = new ArrayList<>();
                List<ResponseInnerStaff> responseInnerDirectors = new ArrayList<>();
                // 按照片中角色名将身份进行区分，并存入不同集合
                for (ResponseInnerStaff responseInnerStaff : allInnerStaff) {
                    if ("导演".equals(responseInnerStaff.getActAs())) {
                        responseInnerDirectors.add(responseInnerStaff);
                    } else {
                        responseInnerActors.add(responseInnerStaff);
                    }
                }
                responseMovie.setActors(responseInnerActors);
                responseMovie.setDirectors(responseInnerDirectors);
                apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(responseMovie);
            }
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败").setData(null);
        }

        return apiResponse;
    }

    /**
     * 1.3 根据条件分页获取指定的电影列表
     * 接口地址:POST /api/v1/movies
     */
    @ApiOperation("1.3 POST: 根据条件分页获取指定的电影列表")
    @PostMapping("/api/v1/movies")
    public ApiResponsePaging<List<ResponseMovie>> movies(@ApiParam("要查询的电影信息") RequestMovie requestMovie) {
        ApiResponsePaging<List<ResponseMovie>> movieApiResponsePaging = new ApiResponsePaging<>();
        List<ResponseMovie> responseMovieList = new ArrayList<>();

        try {
            // 根据请求对象查询
            List<Movie> movieList = movieService.getMovieList(requestMovie);

            for (Movie movie : movieList) {
                ResponseMovie responseMovie = BeanUtil.copyProperties(movie, ResponseMovie.class);

                // 查询电影分类信息
                List<Genre> genreList = genreService.getGenresByMovieId(movie.getId());
                responseMovie.setGenres(genreList);

                List<ResponseInnerStaff> allStaff = staffService.getAllStaffsByMovieId(movie.getId());
                List<ResponseInnerStaff> actors = new ArrayList<>();
                List<ResponseInnerStaff> directors = new ArrayList<>();
                // 按照片中角色名将身份进行区分，并存入不同集合
                for (ResponseInnerStaff responseInnerStaff : allStaff) {
                    if (!"导演".equals(responseInnerStaff.getActAs())) {
                        actors.add(responseInnerStaff);
                    } else {
                        directors.add(responseInnerStaff);
                    }
                }
                responseMovie.setActors(actors);
                responseMovie.setDirectors(directors);
                responseMovieList.add(responseMovie);
            }
            // 计算总的演员条数和分页数量
            Integer totalRow = responseMovieList.size();
            Integer totalPage = Convert.toInt(Math.ceil((double) totalRow / requestMovie.getPagerSize()));

            movieApiResponsePaging.setCode(200).setSuccess(true).setMessage("请求成功").
                    setPageNumber(requestMovie.getPageNumber()).setPageSize(requestMovie.getPagerSize()).
                    setTotalPage(totalPage).setTotalRow(totalRow).setData(responseMovieList);
        } catch (Exception e) {
            e.printStackTrace();
            movieApiResponsePaging.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        return movieApiResponsePaging;
    }

    /**
     * 2.4.1 电影信息增加
     * @param movie 要添加的电影信息
     * @return 返回添加后的结果
     */
    @ApiOperation("2.4.1 POST 电影信息增加")
    @PostMapping("/api/v1/add/movie")
    public ApiResponse<Movie> addMovie(Movie movie) {
        ApiResponse<Movie> apiResponse = new ApiResponse<>();
        try{
            movieService.save(movie);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(movie);
        return apiResponse;
    }

    /**
     * 2.4.2 电影信息修改
     * @param movie 要修改的电影信息
     * @return 返回修改后的结果
     */
    @ApiOperation("2.4.2 POST 电影信息修改")
    @PostMapping("/api/v1/update/movie")
    public ApiResponse<Movie> updateMovie(Movie movie) {
        ApiResponse<Movie> apiResponse = new ApiResponse<>();
        try{
            movieService.updateById(movie);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(movie);
        return apiResponse;
    }

    /**
     * 2.6.1 电影演职员信息增加
     * @param movieCast 要增加的电影职员信息
     * @return 返回添加后的电影职员信息
     */
    @ApiOperation("2.6.1 POST 电影演职员信息添加")
    @PostMapping("/api/v1/add/movieCast")
    public ApiResponse<MovieCast> addMovieCast(MovieCast movieCast) {
        ApiResponse<MovieCast> apiResponse = new ApiResponse<>();
        try{
            movieCastService.save(movieCast);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(movieCast);
        return apiResponse;
    }

    /**
     * 2.6.2 电影演职员信息修改
     * @param movieCast 要修改的电影职员信息
     * @return 返回修改后的电影职员信息
     */
    @ApiOperation("2.6.2 POST 电影演职员信息修改")
    @PostMapping("/api/v1/update/movieCast")
    public ApiResponse<MovieCast> updateMovieCast(MovieCast movieCast) {
        ApiResponse<MovieCast> apiResponse = new ApiResponse<>();
        try{
            movieCastService.updateById(movieCast);
        } catch (Exception e) {
            apiResponse.setCode(500).setSuccess(false).setMessage("请求失败");
        }
        apiResponse.setCode(200).setSuccess(true).setMessage("请求成功").setData(movieCast);
        return apiResponse;
    }
}
